// pages/findTeam/index.js
const db = wx.cloud.database()
const _ = db.command
const teamlistCollection = db.collection('teamlist').where({
  vacancy: _.gt(0)
})
const util = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    teamlist: [],
    openid: '',
    nickname: '',
    src: '',
    tablist: ['一起比赛', '一起租房', '一起出去玩', '其他'],
    currentlist: [],
    currenttabindex: 0
  },
  preimg: function (event) {
    let index = event.currentTarget.dataset.index;
    let src = this.data.teamlist[index].teamimg;

    wx.previewImage({
      current: src,
      urls: [src],
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      console.log(options)
      this.setData({
        currenttabindex: options.type
      })
    }
    this.setData({
      openid: wx.getStorageSync('OPENID')
    })
    this.getallteamlist()
  },
  getallteamlist() {
    teamlistCollection.get().then((res) => {
      let teamlist = res.data.map((item) => {
        let date = util.formatTime(new Date(item.date))
        item.date = date
        return item
      }).reverse()
      teamlist.map(item => {
        item.ismember = item.member.some(item => item.openid === this.data.openid)
        return item
      })
      this.setData({
        teamlist: teamlist
      }, () => {
        this.changelist(this.data.currenttabindex)
      })
    })
  },
  getsearchResult(e) {
    let searchkey = e.detail
    this.setData({
      currentkey: searchkey
    })
    if (searchkey === '') {
      this.getallteamlist()
      return
    }
    wx.showLoading({
      title: '加载中',
    })

    wx.cloud.init({
      env: 'cloud1-5glocnmp62950062'
    })
    wx.cloud.callFunction({
      name: 'searchTeam',
      data: {
        key: this.data.currentkey
      },
      success: (res) => {
        wx.hideLoading()
        let data = res.result.teamlist.data
        let teamlist = data.map((item) => {
          let date = util.formatTime(new Date(item.date))
          item.date = date
          return item
        }).reverse()
        teamlist.map(item => {
          item.ismember = item.member.some(item => item.openid === this.data.openid)
          return item
        })
        this.setData({
          teamlist: teamlist
        })
        this.changelist(this.data.currenttabindex)
      },
      fail: function (err) {
        wx.showToast({
          title: '获取数据失败，请稍后再试',
          icon: 'none',
        })
        console.error(err)
      },
      complete: res => {}
    })

  },
  linkteam: function (e) {
    let index = e.currentTarget.dataset.index;
    let tele = this.data.teamlist[index].phone;
    wx.showModal({
      title: '联系电话为：',
      content: tele,
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '复制',
      confirmColor: '#3CC51F',
      success: (result) => {
        if (result.confirm) {
          wx.setClipboardData({
            data: tele,
          })

        }
      },
      fail: () => {
        return false
      },
      complete: () => {},
    })

  },
  addteam: function (e) {
    let teamid = e.currentTarget.dataset.teamid
    let openid = e.currentTarget.dataset.openid
    if (this.data.openid === openid) {
      wx.showToast({
        title: '不能加入自己发布的组队！',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '确定申请吗？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: (result) => {
        if (result.confirm) {
          wx.navigateTo({
            url: `/pages/application/application?teamid=${teamid}`,
          })

        }
      },
      fail: () => {
        return false
      },
      complete: () => {},
    })
  },
  changetab: function (e) {
    let index = e.currentTarget.dataset.index
    this.changelist(index)
  },
  changelist: function (index) {
    let teamlist = this.data.teamlist,
      str = this.data.tablist[index]
    console.log(str)
    let currentlist = teamlist.filter(item => {
      return item.teamtype == str
    })
    this.setData({
      currenttabindex: index,
      currentlist: currentlist
    })
  },
  gotopublish: function(){
    wx.navigateTo({
      url: '/pages/teamForm/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})